package org.jeecg.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * 请求参数校验配置
 *
 * @author lixingxing
 */
@Configuration
public class ValidationConfig {
    @Bean
    public Validator validator(AutowireCapableBeanFactory autowireCapableBeanFactory) {
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
            .configure()
            .constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapableBeanFactory))
            .failFast(true)
            .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();

        return validator;
    }
}
